# -*- coding: utf-8 -*-
import json
import codecs
import os
# from itemadapter import ItemAdapter

from items import TermsItem

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


class CilibaospiderPipeline:
    # 一次性写到文本文档
    # def process_item(self, item, spider):
    #     try:
    #         with open('fileName', "w") as fo:
    #             print("保存磁力列表到文件:", fo.name)
    #             fo.writelines(ulist)
    #     except:
    #         print("write file failed!")
    #         return "error"
    def __init__(self):
        self.file = codecs.open('save_data_utf8.json', 'w', encoding='utf-8')
        self.file.write('[')

    def process_item(self, item, spider):
        line = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.file.write(line+',')
        return item

    def close_spider(self, spider):
        self.file.seek(-1, os.SEEK_END)
        self.file.truncate();
        self.file.write(']')
        self.file.close()
